/**
 * Created by bianlian on 9/4.
 */
import { Platform, AsyncStorage } from 'react-native';
import { createStore, applyMiddleware, compose } from 'redux';
import thunkMiddleware from 'redux-thunk';
import devTools from 'remote-redux-devtools';
import rootReducer from '../reducers/index';
//异步
const createStoreWithMiddleware = applyMiddleware(thunkMiddleware)(createStore);

async function configureStore(onComplete = () => { }) {
    const enhancer = compose(
        applyMiddleware(thunkMiddleware),
        devTools({
            name: Platform.OS,
            hostname: '192.168.186.149',
            port: 8157
        })
    );
    //将Store和reducer绑定
  let store;

  try {
    const value = await AsyncStorage.getItem('state');
    if (value) {
      console.warn('AsyncStorage value: '+ value);
      store = createStoreWithMiddleware(rootReducer, JSON.parse(value), enhancer);
    }
    else {
      // alert('hello');
      store = createStoreWithMiddleware(rootReducer, {}, enhancer);

      // console.log('Initialized with no selection on disk.');
    }
    onComplete(store);
  }
  catch (error) {
    console.error('AsyncStorage error: ', error.message);
  }
  return store;

}
module.exports = configureStore;
